package com.yhcui.monitor.utils;

import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;
import org.springframework.util.StringUtils;

@Slf4j
public class TraceUtils {

    public static final String TRACE_ID = "traceId";
    public static final ZfIdGenerator  zfIdGenerator = ZfIdGenerator.getStandAloneIdGenerator();

    public static void createTraceId() {
        createTraceId(null);
    }
    public static void createTraceId(String requestTraceId) {
        String traceId = MDC.get(TRACE_ID);
        if (!StringUtils.hasText(traceId)) {
            if (StringUtils.hasText(requestTraceId)) {
                traceId =  requestTraceId;
            } else {
//                traceId = UUID.randomUUID().toString().replaceAll("-", "").toLowerCase();
                traceId = zfIdGenerator.nextId() + "";
                log.debug("create traceId :{}", traceId);
            }

            MDC.put(TRACE_ID, traceId);
        }
    }
    public static void destroyTraceId() {
        MDC.remove(TRACE_ID);
    }

}
